<template>
	<!--自定义动态tarbar 在需要展示tarbar的页面引入即可
    <tabBar selectedIndex = 0></tabBar>

    第一种：
     * 根据身份判断 动态显示tarbar内容

    第二种：
     * 有具体的页面传递需要动态显示的tarbar，也是支持的

     设计思路：tarbar组件根据页面传递的动态内容显示不同tarbar效果
  -->
	<view class="tab">
		<view v-for="(item,index) in list" :key="index" class="tab-item" @click="switchTab(item, index)">
			<image class="tab_img" :src="currentIndex == index ? item.selectedIconPath : item.iconPath"></image>
			<view class="tab_text" :style="{color: currentIndex == index ? selectedColor : color}">
				{{ item.text }}
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: "rarbar",
		props: {
			selectedIndex: { // 当前选中的tab index
				default: 1
			},
		},
		data() {
			return {
				color: "#666666",
				selectedColor: "#FE2C55",
				list: [],
				currentIndex: 0,
			}
		},
		created() {
			this.currentIndex = this.selectedIndex;

			let _this = this
			/**
			 * 第一种：
			 * 根据身份判断 动态显示tarbar内容
			 * 登录从后端获取值设置值
			 */
			if (uni.getStorageSync('identify') == 'tom') {
				//角色1
				_this.list = [{
						"pagePath": "/pages/index/index",
						"iconPath": "/static/tabbar/privateDomain.png",
						"selectedIconPath": "/static/tabbar/privateDomain-active.png",
						"text": "首页"
					},
					{
						"pagePath": "/pages/square/square",
						"iconPath": "/static/tabbar/planet.png",
						"selectedIconPath": "/static/tabbar/planet-active.png",
						"text": "广场"
					},
					{
						"pagePath": "/pages/user/user",
						"iconPath": "/static/tabbar/user2.png",
						"selectedIconPath": "/static/tabbar/user2-active.png",
						"text": "我的"
					}
				]
			} else {
				//角色2 item.selectedIconPath : item.iconPath"
				_this.list = [{
						"pagePath": "/pages/index/index",
						"iconPath": "/static/tabbar/privateDomain.png",
						"selectedIconPath": "/static/tabbar/privateDomain-active.png",
						"text": "私域"
					},
					{
						"pagePath": "/pages/square/square",
						"iconPath": "/static/tabbar/planet.png",
						"selectedIconPath": "/static/tabbar/planet-active.png",
						"text": "广场"
					},
					{
						"pagePath": "/pages/orders/orders",
						"iconPath": "/static/tabbar/public.png",
						"selectedIconPath": "/static/tabbar/public-active.png",
						"text": "公共商品池"
					},
					{
						"pagePath": "/pages/user/user",
						"iconPath": "/static/tabbar/user2.png",
						"selectedIconPath": "/static/tabbar/user2-active.png",
						"text": "我的"
						// "events":{
						// 	"bindtap":"onTabItemTap"
						// }
					}
				]
			}

			/**
			 * 第二种：
			 * 有具体的页面传递需要动态显示的tarbar，也是支持的
			 */

		},
		methods: {
			switchTab(item, index) {
				console.log("传递来的", index);
				// this.currentIndex = index;
				let url = item.pagePath;
				// console.log("url", url)

				// 跳转tarbar 使用这种方式
				// this.$tab.switchTab(url)
				uni.switchTab({
					url
				})

				// 跳转普通页面使用郑重方式
				// this.$tab.redirectTo(url)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.tab {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		height: 50px;
		background: white;
		display: flex;
		justify-content: center;
		align-items: center;
		padding-bottom: env(safe-area-inset-bottom); // 适配iphoneX的底部

		.tab-item {
			flex: 1;
			text-align: center;
			display: flex;
			justify-content: center;
			align-items: center;
			flex-direction: column;

			.tab_img {
				width: 20px;
				height: 20px;
			}

			.tab_text {
				font-size: 10px;
				margin-top: 5px;
				margin-bottom: 2px;
			}
		}
	}
</style>